// =================== 定义全局变量 ======================
const USER_CANDIDATE = 0;
const USER_PUBLISHER = 1;
const REQUEST_HEADER = 'USER_TOKEN';

let currentUserStatus = localStorage.getItem('currentUserStatus'); // 从本地获取
let avatarUrl = 'image/avatar01.jpeg'; // 默认头像
let currentUser;  // 当前登录用户
let pageSize = 6; // 默认分页大小

// 当前访问的职位Id
let currentJobId;

// ============================ AJAX全局设置 ===============================
$.ajaxSetup({
    headers: {
        'USER_TOKEN': localStorage.getItem(REQUEST_HEADER)
    },
    contentType: 'application/json',
    // contentType: 'application/x-www-form-urlencoded',
    statusCode: {
      401: function() {
        console.info('401错误，请重新登录',);
        // 跳转到登录页
        location.assign('sign-in.html');
      }
    },
    error : function() {
      $.toast({
          heading: '错误',
          text: '出错了，请联系管理员',
          icon: 'error'
        });
    }
});

// ============================ 添加导航激活效果 ===========================
function changeNavActive (item) {
    // 判断当前是否为激活状态
    if (item.hasClass('active') == false) {
      let activeLiEl = $('#navbar-menu .active');
      activeLiEl.removeClass('active');
      item.addClass('active');
      console.log('切换身份');
    }
}

// ============================ 删除导航激活效果 ===========================
function removeNavActive () {
    // 判断当前是否为激活状态
    let activeLiEl = $('#navbar-menu .active');
    if (activeLiEl) {
      activeLiEl.removeClass('active');
    }
}

// ===================== 导航栏选项卡 ===================
$(function () {
  // 切换身份，默认求职者
  $('#find_positions').click(function () {
    changeNavActive($(this));
    // 当前为应聘者，访问职位列表
    currentUserStatus = USER_CANDIDATE;
    localStorage.setItem('currentUserStatus', currentUserStatus);
  });
  // 发布者
  $('#find_candidate').click(function () {
    changeNavActive($(this));
    // 当前为发布者，访问牛人列表
    currentUserStatus = USER_PUBLISHER;
    // 设置到本地，下次读取
    localStorage.setItem('currentUserStatus', currentUserStatus);
  });

  // ============================ 访问者身份初始化 ============================
  if (!currentUserStatus) {
    currentUserStatus = USER_CANDIDATE;
    changeNavActive($('#find_positions'));
    // 保存到本地
    localStorage.setItem('currentUserStatus', currentUserStatus);
  } else {
    if (currentUserStatus == USER_CANDIDATE) {
      // 职位列表
      changeNavActive($('#find_positions'));
    } else if (currentUserStatus == USER_PUBLISHER) {
      // 牛人列表
      changeNavActive($('#find_candidate'));
    }
  }
});

// =============== toast ================
// info
function toastInfo(message) {
  $.toast({
    heading: '提示',
    text: message,
    icon: 'info'
  });
}

// success
function toastSuccess(message) {
  $.toast({
    heading: '成功',
    text: message,
    icon: 'success'
  });
}

// warning
function toastWarning(message) {
  $.toast({
    heading: '警告',
    text: message,
    icon: 'warning'
  });
}

// error
function toastError(message) {
  $.toast({
    heading: '错误',
    text: message,
    icon: 'error'
  });
}


